﻿using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Project13776616780_01.Model;
using Project13776616780_01.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project13776616780_01.Pages
{
    public partial class Recyle
    {
        [Inject]
        DocsDbContext dbContext { get; set; }
        private List<ProductModel> products = new List<ProductModel>();
        private HashSet<object> selectedProducts = new HashSet<object>();
        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            await RefreshDataAsync();
        }

        private async Task RestoreAsync()
        {
            if (!selectedProducts.Any())
            {
                Toast("没有选择任何藏品");
                return;
            }

            foreach (ProductModel item in selectedProducts)
            {
                var ep = dbContext.Set<Product>().FirstOrDefault(x => x.Id == item.Id);
                if (ep == null)
                {
                    continue;
                }
                ep.IsDeleted = false;
            }
            await dbContext.SaveChangesAsync();
            await RefreshDataAsync();
            Toast("还原成功");
        }
        private async Task DeleteAsync()
        {
            if (!selectedProducts.Any())
            {
                Toast("没有选择任何藏品");
                return;
            }

            var r = await ConfirmAsync("彻底删除后无法恢复，确认删除？");
            if (r != Blazui.Component.MessageBoxResult.Ok)
            {
                return;
            }
            foreach (ProductModel item in selectedProducts)
            {
                var ep = dbContext.Set<Product>().FirstOrDefault(x => x.Id == item.Id);
                if (ep == null)
                {
                    continue;
                }
                dbContext.Set<Product>().Remove(ep);
            }
            await dbContext.SaveChangesAsync();
            await RefreshDataAsync();
            Toast("还原成功");
        }
        private async Task EmptyAsync()
        {
            if (!products.Any())
            {
                Toast("没有任何藏品");
                return;
            }

            var r = await ConfirmAsync("这将彻底删除所有藏品，且无法恢复，确认删除？");
            if (r != Blazui.Component.MessageBoxResult.Ok)
            {
                return;
            }
            foreach (ProductModel item in products)
            {
                var ep = dbContext.Set<Product>().FirstOrDefault(x => x.Id == item.Id);
                if (ep == null)
                {
                    continue;
                }
                dbContext.Set<Product>().Remove(ep);
            }
            await dbContext.SaveChangesAsync();
            await RefreshDataAsync();
            Toast("清空成功");
        }

        private async Task RefreshDataAsync()
        {
            var authors = dbContext.Authors.ToDictionary(x => x.Id, x => x.Name);
            var sizes = dbContext.Sizes.ToDictionary(x => x.Id, x => x.Name);
            products = await dbContext.Set<Product>().Where(x => x.IsDeleted).OrderByDescending(x => x.Id).Select(x => new ProductModel()
            {
                Author = authors.GetValueOrDefault(x.AuthorId, "未知"),
                CreationTime = x.CreationTime,
                IdNumber = x.IdNumber,
                Image = x.Image,
                Name = x.Name,
                Id = x.Id,
                Size = sizes.GetValueOrDefault(x.SizeId, "未知"),
                Description = x.Description,
                DescriptionTitle = x.DescriptionTitle,
                CategoryId = x.CategoryId
            }).ToListAsync();
            await InvokeAsync(Refresh);
        }
    }
}
